var MaterialSetting = function () {
  var template = /* html */ `
    <div class="material-setting" v-loading.fullscreen.lock="loading">
      <div style="font-size:18px;margin-bottom:5px;">物料入库可调整配置</div>
      <el-form inline size="small">
        <el-form-item>
          <el-input style="width:210px" v-model="searchValue" placeholder="请输入物料名称">
            <el-button slot="append" icon="el-icon-search" title="查询物料" @click="searchData"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="saveConfig">保存配置</el-button>
        </el-form-item>
      </el-form>
      <div class="material-setting-table-div">
        <el-table fit stripe border ref="materialTable" :data="materialList" style="width:100%;" height="parent">
          <el-table-column sortable label="物料名称" prop="materialName" min-width="120px" header-align="center"></el-table-column>
          <el-table-column label="调整数量" prop="adJustInCount" min-width="100px" header-align="center">
            <template slot-scope="scope">
              <el-input-number is-decimal-zero class="table-input" v-model="scope.row.adJustInCount" size="mini" :controls="false" :min="0" :precision="2">
              </el-input-number>
            </template>
          </el-table-column>
          <el-table-column label="调整单价" prop="adJustInPrice" min-width="100px" header-align="center">
            <template slot-scope="scope">
              <el-input-number is-decimal-zero class="table-input" v-model="scope.row.adJustInPrice" size="mini" :controls="false" :min="0" :precision="4">
              </el-input-number>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
  `;

  return {
    props: {
      materials: {
        type: Array,
        required: true
      }
    },

    watch: {
      materials: function () {
        this.searchData(true);
      }
    },

    data: function () {
      return {
        loading: false,
        searchValue: '',
        oldSearchValue: '',
        materialList: []
      };
    },

    created: function () {
      var self = this;
      this.$nextTick(function () {
        self.materialList = self.materials;
      });
    },

    methods: {
      searchData: function (checkSame) {
        var searchValue = this.searchValue;
        if (!checkSame) {
          if (searchValue === searchValue) {
            return;
          }
        }
        var materials = this.materials;
        if (searchValue !== '') {
          searchValue = searchValue.toLowerCase();
          materials = _.filter(materials, function (material) {
            var materialName = material.materialName;
            materialName = materialName.toLowerCase();
            return materialName.indexOf(searchValue) !== -1;
          });
        }
        this.materialList = materials;
        this.oldSearchValue = searchValue;
      },

      saveConfig: function () {
        var materialList = this.materials;
        var postDataList = [];
        for (var i = 0; i < materialList.length; i++) {
          var material = materialList[i];
          var postData = material.getObject();
          if (postData === null) {
            continue;
          }
          postDataList.push(postData);
        }

        this.loading = true;
        var self = this;
        ajaxRequest('company/setMaterialConfig', {
          jsonArray: JSON.stringify(postDataList)
        }).then(function () {
          self.$alert('物料入库可调整配置成功', '信息');
          self.loading = false;
        }).catch(function () {
          self.loading = false;
        });
      }
    },

    template: template
  };
}();
